package com.cyt.work.association.queue;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.exception.base.BaseException;
import com.cyt.base.redis.RedisService;
import com.cyt.base.util.AssociationConstant;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.work.association.model.po.WxAssociationSign;
import com.cyt.work.association.service.WxAssociationSignService;
import com.cyt.work.association.service.impl.WxAssociationSignServiceImpl;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * @author taocongcong
 * @create 2019-11-26 14:33
 */
public class SignConsumer implements MessageListener {

    private static Logger log = LoggerFactory.getLogger(WxAssociationSignServiceImpl.class);
    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Resource
    private RedisService redisService;
    @Resource
    private WxAssociationSignService wxAssociationSignService;

    @Override
    public void onMessage(Message msg) {
        try {
            // 通过线程睡眠测试数据访问
            /*try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
            JsonNode jsonData = MAPPER.readTree(msg.getBody());
            String signId = jsonData.get("signId").asText();
            String applyId = jsonData.get("applyId").asText();
            String associationId = jsonData.get("associationId").asText();
            String associationNewId = jsonData.get("associationNewId").asText();
            String associationNewDetailId = jsonData.get("associationNewDetailId").asText();
            String userId = jsonData.get("userId").asText();
            Date now = new Date();
            WxAssociationSign wxAssociationSign = new WxAssociationSign();
            wxAssociationSign.setId(signId);
            wxAssociationSign.setApplyId(applyId);
            wxAssociationSign.setAssociationId(associationId);
            wxAssociationSign.setAssociationNewId(associationNewId);
            wxAssociationSign.setAssociationDetailId(associationNewDetailId);
            wxAssociationSign.setAssociationUserId(userId);
            wxAssociationSign.setSignTime(now);
            wxAssociationSign.setStatus(BaseConstant.ASSOCITATION_SIGN_STATE_SUCCESSD);
            wxAssociationSign.setRemoveTag(DictCode.BASE_REMOVE_TAG_NORMAL);
            try {
                int flag = wxAssociationSignService.insert(wxAssociationSign);
                if (flag <= 0) {
                    // 回滚redis操作
                    redisService.increment(AssociationConstant.WX_ASSOCIATION_NEW_DETAIL_NUM + associationNewDetailId, 1);
                    redisService.increment(AssociationConstant.WX_ASSOCIATION_NEW_STU_NUM + associationNewId + "_" + userId, 1);
                    // 删除社团
                    redisService.del(AssociationConstant.WX_ASSOCIATION_NEW_STU_DETAIL + associationNewDetailId + "_" + userId);
                    log.info("数据库保存学生主键{}，入团{}异常，回滚操作", userId, associationNewDetailId);
                }
            } catch (Exception e) {
                redisService.increment(AssociationConstant.WX_ASSOCIATION_NEW_DETAIL_NUM + associationNewDetailId, 1);
                redisService.increment(AssociationConstant.WX_ASSOCIATION_NEW_STU_NUM + associationNewId + "_" + userId, 1);
                // 删除社团
                redisService.del(AssociationConstant.WX_ASSOCIATION_NEW_STU_DETAIL + associationNewDetailId + "_" + userId);
                // 回滚redis操作
                log.info("数据库保存学生主键{}，入团{}异常，回滚操作", userId, associationNewDetailId);
                throw new BaseException(ErrorCode.CODE_1, "数据库出现问题，请等待处理");
            }

            log.info("学生主键为{}报名社团成功，招新关联社团表主键为{}，时间是：{}", userId, associationNewDetailId, DateUtil.formatDateTime(now));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
